package kevin.workplan.util;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

public class PageUtil {

	
	/** 
	 * 设置分页参数
	 * @param count
	 * @param pagesize
	 */
	public static int setPage(HttpServletRequest request, int curpage, int count, int pagesize, List dataList) {
		request.setAttribute("curpage", curpage);
		request.setAttribute("count", count);
		request.setAttribute("pagesize", pagesize);
		int totalpage = count / pagesize;
		if( (totalpage*pagesize) != count)
			totalpage++;
		request.setAttribute("totalpage", totalpage);
		request.setAttribute("dataList", dataList);
		return totalpage;
		
	}
	/** 
	 * 提取分页参数
	 * @param request
	 * @return
	 */
	public static int getPagesize(HttpServletRequest request){
		int pagesize = Constant.PAGESIZE;
		String pagesizeStr = request.getParameter("pagesize");
		if(!Util.empty(pagesizeStr))
			pagesize = Integer.parseInt(pagesizeStr);
		String rows = request.getParameter("rows");
		if(!Util.empty(rows))
			pagesize = Integer.parseInt(rows);
		return pagesize;
	}
	
	public static Integer getInteger(HttpServletRequest request, String name) {
		String v = request.getParameter(name);
		if(!Util.empty(v))
			return Integer.parseInt(v);
		return null;
	}
	public static Long getLong(HttpServletRequest request, String name) {
		String v = request.getParameter(name);
		if(!Util.empty(v))
			return Long.parseLong(v);
		return null;
	}
	
	
	public static void genPage(HttpServletRequest request, int curpage, int count, int pagesize, List dataList) {
		int totalpage = setPage(request, curpage, count, pagesize, dataList);
		String page = page(totalpage, curpage, count);
		request.setAttribute("page", page);
	}
	/** 
	 * 提取分页参数
	 * @param request
	 * @return
	 */
	public static int getCurpage(HttpServletRequest request){
		int curpage = 1;
		String curpageStr = request.getParameter("curpage");
		if(!Util.empty(curpageStr))
			curpage = Integer.parseInt(curpageStr);
		String pageStr = request.getParameter("page");
		if(!Util.empty(pageStr))
			curpage = Integer.parseInt(pageStr);
		return curpage;
	}
	
	/**
	 * 生成前台分页html
	 * @param totalPage
	 * @param curpage
	 * @param total
	 * @return
	 */
	public static String page(int totalPage, int curpage, int total) {
		String preClass = "";
		int pre = 1;
		if (curpage == 1) {
			preClass = "k-disabled";
		} else {
			pre = curpage - 1;
		}

		String html = "<div class='k-page'><input id=\"curpage\" type=\"hidden\" name=\"curpage\" value=\""+curpage+"\"/><span class='k-first k-click " + preClass
				+ "' curpage='1'>首页</span><span class='k-pre k-click " + preClass + "' curpage='"
				+ pre + "'>上一页</span>";

		int count = 10;
		int start = 1;
		int end = totalPage;
		if (totalPage >= count) {
			int half = 4;
			start = curpage - half;
			end = curpage + half;
			if (start <= 0) {
				int c = 1 - start;
				start = 1;
				end += c;
			}
			if (end > totalPage) {
				int c = end - totalPage;
				end = totalPage;
				start -= c;
			}
		}

		for (int i = start; i <= end; i++) {
			String now = "";
			if (curpage == i)
				now = "k-curpage";
			html += "<span class='k-pagenum k-click " + now + "' curpage='" + i + "'>" + i
					+ "</span>";
		}
		String nextClass = "";
		int next = totalPage;
		if (curpage >= totalPage) {
			nextClass = "k-disabled";
		} else {
			next = curpage + 1;
		}

		html += "<span class='k-next k-click " + nextClass + "' curpage='" + next
				+ "'>下一页</span><span class='k-last k-click " + nextClass + "' curpage='"
				+ totalPage + "'>末页</span><span class='k-total'>共"+totalPage+"页"+total+"条记录</span></div>";

		return html;
	}
}
